local BenzhenRoom = {}

BenzhenRoom.__index = BenzhenRoom

function BenzhenRoom.Start(gameObject)
	local object = BenzhenRoom:new()
	object.gameObject = gameObject
	
	--[[
	local function onPointerEnter(eventData)
		print("onPointerEnter ", object.gameObject.name)
		print("onPointerEnter ", eventData.position.x, eventData.position.y)
	end

	local function onPointerExit(eventData)
		print("onPointerExit ", object.gameObject.name)
		print("onPointerExit ", eventData.position.x, eventData.position.y)
	end
	]]

	object.transform = gameObject:GetComponent(typeof(UnityEngine.RectTransform))

	local lastPos
	local floorPos

	local function placeFloorImage(x,y)
		local gridX, gridY = gameInstance.benzhen.benzhenLayer:getGrid(x, y)
		
		floorPos = gameInstance.benzhen.floorImage.localPosition
		floorPos.x, floorPos.y = gameInstance.benzhen.benzhenLayer:getPosition(gridX, gridY, 1,1)
		gameInstance.benzhen.floorImage.localPosition = floorPos
	end

	local function onPointerDown(eventData)
		print("onPointerDown ", object.gameObject.name)
		print("onPointerDown ", eventData.position.x, eventData.position.y)
	end

	local function onPointerClick(eventData)
		print("onPointerClick ", object.gameObject.name)
		print("onPointerClick ", eventData.position.x, eventData.position.y)
	end

	local function onBeginDrag(eventData)
		print("onBeginDrag ", object.gameObject.name)
		print("onBeginDrag ", eventData.position.x, eventData.position.y)

		lastPos = object.transform.localPosition
		local _, curPos = UnityEngine.RectTransformUtility.ScreenPointToLocalPointInRectangle(object.transform.parent, eventData.position, eventData.pressEventCamera, pos)
		object.transform.localPosition = curPos

		gameInstance.benzhen.floorImage.gameObject:SetActive(true)
		placeFloorImage(curPos.x, curPos.y)

	end

	local function onDrag(eventData)
		print("onDrag ", object.gameObject.name)
		print("onDrag ", eventData.position.x, eventData.position.y)
		local pos = UnityEngine.Vector3(0,0,0)
		--[[
		local _,curPos = UnityEngine.RectTransformUtility.ScreenPointToWorldPointInRectangle(object.transform.parent, eventData.position, eventData.pressEventCamera, pos)
		print("curPos", curPos.x, curPos.y)
		object.transform.position = curPos
		]]
		local _, curPos = UnityEngine.RectTransformUtility.ScreenPointToLocalPointInRectangle(object.transform.parent, eventData.position, eventData.pressEventCamera, pos)
		object.transform.localPosition = curPos

		placeFloorImage(curPos.x, curPos.y)
		
	end

	local function onEndDrag(eventData)
		print("onEndDrag ", object.gameObject.name)
		print("onEndDrag ", eventData.position.x, eventData.position.y)

		object.transform.localPosition = floorPos

		gameInstance.benzhen.floorImage.gameObject:SetActive(false)
	end
	
	local luatouch = object.gameObject:GetComponent(typeof(LuaTouchComponent))
	--luatouch:RegisterFunction(UnityEngine.EventSystems.EventTriggerType.PointerEnter, onPointerEnter)
	--luatouch:RegisterFunction(UnityEngine.EventSystems.EventTriggerType.PointerExit, onPointerExit)
	luatouch:RegisterFunction(UnityEngine.EventSystems.EventTriggerType.PointerDown, onPointerDown)
	luatouch:RegisterFunction(UnityEngine.EventSystems.EventTriggerType.PointerClick, onPointerClick)
	luatouch:RegisterFunction(UnityEngine.EventSystems.EventTriggerType.BeginDrag, onBeginDrag)
	luatouch:RegisterFunction(UnityEngine.EventSystems.EventTriggerType.Drag, onDrag)
	luatouch:RegisterFunction(UnityEngine.EventSystems.EventTriggerType.EndDrag, onEndDrag)

	--object.luatouch = luatouch

	print("BenzhenRoom.Start")

	return object
end

function BenzhenRoom.Destroy(gameObject, object)
	-- body
end

function BenzhenRoom:new()
	local object = {}
	setmetatable(object, BenzhenRoom)

	return object
end

return BenzhenRoom